package rexsee.core.widget;

import rexsee.core.menu.PopMenu;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;

public class TabBar extends Div {

	public final LinearLayout mContainer;
	private final HScrollView mScrollerX;
	private final ScrollView mScrollerY;

	public TabBar(final Context context, PopMenu popMenu) {
		this(context, popMenu, "");
	}
	public TabBar(final Context context, PopMenu popMenu, String style) {

		super(context);

		setDivId(popMenu.id);
		styleSheet = popMenu.styleSheet.clone().parseStyle(style);

		mContainer = new LinearLayout(context);
		mContainer.setGravity(styleSheet.getGravity());
		if (styleSheet.orientation.equalsIgnoreCase("x") || styleSheet.orientation.equalsIgnoreCase("horizontal")) {
			mContainer.setOrientation(LinearLayout.HORIZONTAL);
		} else {
			mContainer.setOrientation(LinearLayout.VERTICAL);
		}
		for (int i = 0; i < popMenu.items.size(); i++) {
			final Tab tab = new Tab(context, popMenu.items.get(i));
			mContainer.addView(tab);
		}

		mScrollerX = new HScrollView(context) {
			@Override
			public int getSolidColor() {
				try {
					return Color.parseColor(styleSheet.fading_edge_x_color);
				} catch (Exception e) {
					return Color.BLACK;
				}
			}
		};
		mScrollerY = new ScrollView(context) {
			@Override
			public int getSolidColor() {
				try {
					return Color.parseColor(styleSheet.fading_edge_y_color);
				} catch (Exception e) {
					return Color.BLACK;
				}
			}
		};

		try {

			if (styleSheet.overflow_x.equalsIgnoreCase("visible") && styleSheet.overflow_y.equalsIgnoreCase("visible")) {
				mScrollerX.addView(mContainer);
				mScrollerY.addView(mScrollerX);
				this.addDivChild(mScrollerY);
			} else if (styleSheet.overflow_x.equalsIgnoreCase("visible")) {
				mScrollerX.addView(mContainer);
				this.addDivChild(mScrollerX);
			} else if (styleSheet.overflow_y.equalsIgnoreCase("visible")) {
				mScrollerY.addView(mContainer);
				this.addDivChild(mScrollerY);
			} else {
				this.addDivChild(mContainer);
			}

			setBarStyle();

		} catch (Exception e) {
			Toast.makeText(context, "TabBar:" + e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
		}
	}

	public void setBarStyle(String style) {
		styleSheet.parseStyle(style);
		setBarStyle();
	}
	public void setBarStyle() {
		try {
			if (styleSheet.overflow_x.equalsIgnoreCase("visible")) {
				mScrollerX.setHorizontalFadingEdgeEnabled(true);
				mScrollerX.setFadingEdgeLength(Integer.parseInt(styleSheet.fading_edge_x_length.toLowerCase().replaceAll("px", "")));
				if (styleSheet.scrollbar_x.equalsIgnoreCase("hidden")) {
					mScrollerX.setHorizontalScrollBarEnabled(false);
				} else {
					mScrollerX.setHorizontalScrollBarEnabled(true);
					mScrollerX.setScrollBarStyle(styleSheet.getScrollbarXStyle());
					mScrollerX.setScrollbarFadingEnabled((styleSheet.scrollbar_x.equalsIgnoreCase("visible")) ? false : true);
				}
			}
			if (styleSheet.overflow_y.equalsIgnoreCase("visible")) {
				mScrollerY.setVerticalFadingEdgeEnabled(true);
				mScrollerY.setFadingEdgeLength(Integer.parseInt(styleSheet.fading_edge_y_length.toLowerCase().replaceAll("px", "")));
				if (styleSheet.scrollbar_y.equalsIgnoreCase("hidden")) {
					mScrollerY.setVerticalScrollBarEnabled(false);
				} else {
					mScrollerY.setVerticalScrollBarEnabled(true);
					mScrollerY.setScrollBarStyle(styleSheet.getScrollbarYStyle());
					mScrollerY.setScrollbarFadingEnabled((styleSheet.scrollbar_y.equalsIgnoreCase("visible")) ? false : true);
				}
			}
		} catch (Exception e) {
			Toast.makeText(mContext, "TabBar:" + e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
		}
		setDivStyle();
	}

	public Tab getTab(int index) {
		return (index >= mContainer.getChildCount()) ? null : (Tab) mContainer.getChildAt(index);
	}
	public boolean selectTab(int index) {
		Tab tab = getTab(index);
		if (tab != null) {
			unselectTabs();
			tab.setSelected(true);
			scrollToChild(tab);
			return true;
		} else {
			return false;
		}
	}
	public Tab getTab(String id) {
		for (int i = 0; i < mContainer.getChildCount(); i++) {
			Tab tab = (Tab) mContainer.getChildAt(i);
			if (id.equals(tab.getDivId())) return tab;
		}
		return null;
	}
	public boolean selectTab(String id) {
		return selectTabs(new String[]{id});
	}
	public boolean selectTabs(String[] ids) {
		if (ids == null) return false;
		unselectTabs();
		boolean rtn = false;
		for (int i = 0; i < ids.length; i++) {
			String id = ids[i];
			Tab tab = getTab(id);
			if (tab != null) {
				tab.setSelected(true);
				scrollToChild(tab);
				rtn = true;
			}
		}
		return rtn;
	}
	public void unselectTabs() {
		for (int i = 0; i < mContainer.getChildCount(); i++) {
			Tab tab = (Tab) mContainer.getChildAt(i);
			tab.setSelected(false);
		}
	}
	public final void scrollToChild(View child) {
		if (styleSheet.overflow_x.equalsIgnoreCase("visible")) {
			int x = child.getLeft() - mScrollerX.getScrollX();
			if (x < 0) {
				mScrollerX.scrollBy(x, 0);
			}
			if (x > (getWidth() - child.getWidth())) {
				mScrollerX.scrollBy(x - (getWidth() - child.getWidth()), 0);
			}
		}
		if (styleSheet.overflow_y.equalsIgnoreCase("visible")) {
			int y = child.getTop() - mScrollerY.getScrollY();
			if (y < 0) {
				mScrollerY.scrollBy(0, y);
			}
			if (y > (getHeight() - child.getHeight())) {
				mScrollerY.scrollBy(0, y - (getHeight() - child.getHeight()));
			}
		}
	}

	public void destroy() {
		for (int i = 0; i < mContainer.getChildCount(); i++) {
			try {
				Tab tab = (Tab) mContainer.getChildAt(i);
				tab.destroy();
			} catch (Exception e) {
			}
		}
		styleSheet.destroy();
	}

}
